// 用电子级占比排名
var numberFormate = iot.getNumberFormat("0.00");
dev.action = function(resp,param){
    //获取工厂名称
    var factoryName = getFactoryName(param.workSpaceName);
    //是否算总还是单
    var totalOrSingle = param.selectType;
    if (totalOrSingle == null){
        totalOrSingle = "total";
    }
    var theTime = param.time;
    var workSpace = param.workSpace;
    var datas = iot.list();
    var legend = iot.list();
    var db = iot.db();
    var strucResult = db.qry("select es.id, es.name from energy_struc es where es.pid = " + workSpace);

    var strucs = iot.list();
    strucResult.forAll(function(item){
        strucs.add(item.name);
    });
    var resultChildRank = db.qry("select es.name, sum(win.dvalue) sum from energy_struc es\n" +
        " left join energy_struc_prop esp on es.id = esp.energy_struc_id\n" +
        " left join iot_dev_data_win win on esp.dev_id = win.dev_id  and esp.name = win.data_type \n" +
        " where es.pid = " + workSpace + " and esp.type = 1 and esp.field_type = '用量'\n" +
        " and win.the_time like '" + theTime + "%' group by es.id");

    //获取产量
    var yieldId = getFactoryYieldId(factoryName)

    var yieldResult = getFactoryYield(yieldId, theTime)

    var totalYield = yieldResult.totalSum;
    for (var index in strucs) {
        var sum = 0;
        resultChildRank.forAll(function(item){
            if (item && item.name && item.name.equals(strucs[index])) {
                sum = item.sum;
            }
        })

        //判断当前选择总数还是单车
        if (totalOrSingle.equals("total")) {
            datas.add({name: strucs[index], value: numberFormate.format(sum)});
            legend.add(strucs[index]);
        }else if (totalOrSingle.equals("single")) {
            if (sum && totalYield){
                sum = sum/totalYield;
            }
            datas.add({name: strucs[index], value: numberFormate.format(totalYield === null ? 0 : sum)});
            legend.add(strucs[index]);
        }
    }

    var jsData = {
        datas : datas,
        legend: legend
    };
    resp.setJsData(jsData);
}